package com.suwish.pc.ui.util;

import java.io.IOException;

import javax.swing.JFileChooser;

import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.TimeoutException;
import com.suwish.device.util.DeviceUtils;
import com.suwish.device.util.Platform;
import com.suwish.pc.ui.dialog.DdmsDeviceSelectDialog;
import com.suwish.pc.ui.main.MainFrame;

/**
 * 
 * 顾名思义，面向设备的工具类，但是与{@link DeviceUtils}存在设计上的差异。</p>
 * 
 * 
 * 即<code>DeviceUtils</code>无需考虑UI层的调用便利性而是仅仅考虑自己如何高效的
 * 执行和隐藏底层对设备的操作实现。尽于此<code>DeviceHelper</code>更像是一个UI
 * 和底层的隔离，初步设计在已下场景。</p>
 * 
 * 同一个事件在不同控件中相互调用，控件处理之后的数据部适合直接使用<code>DeviceUtils</code>
 * 其中需要一些额外的转换，此时为了避免同一个甚至一批的重复代码，将这些操作统一转移到
 * 一个工具方法(Helper)中。当然如果在UI层单独写公用方法也未尝不可，达到UI和数据处理的分离目的
 * 即可。
 * </p>
 * 
 * 
 * 
 * @author Min
 *
 */
public final class DeviceHelper {

	
	private DeviceHelper(){}
	
	/**
	 * 
	 * 返回设备的简单描述。</p>
	 * 需要更加详细的描述参见{@link DeviceUtils#getDeviceInfo4List(IDevice)}
	 * 
	 * @param device
	 * @return
	 */
	public static String getDeviceDescName(IDevice device){
		return device.isEmulator() ? device.getSerialNumber() : 
			("[" + device.getProperty(Platform.PROP_OR_PRODUCT_MANUFACTURE) + " " +
					"" + device.getProperty(Platform.PROP_OR_PRODUCT_MODEL) +  "]");
	}
	
	public static void installAPK(IDevice device) 
			throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException{
		JFileChooser chooser = new JFileChooser();
		int result = chooser.showOpenDialog(MainFrame.getInstance());
		if(result == JFileChooser.CANCEL_OPTION) return;
		DeviceUtils.installAPK(device, chooser.getSelectedFile().getAbsolutePath(), true);
	}
	public static void uninstallApk(IDevice device){}
	public static void installAPK() 
			throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException{
		DdmsDeviceSelectDialog dialog = new DdmsDeviceSelectDialog(MainFrame.getInstance());	
		dialog.setVisible(true);
		IDevice device = dialog.getSelectedDevice();
		if(device == null)return;
		JFileChooser chooser = new JFileChooser();
		int result = chooser.showOpenDialog(MainFrame.getInstance());
		if(result == JFileChooser.CANCEL_OPTION) return;
		DeviceUtils.installAPK(device, chooser.getSelectedFile().getAbsolutePath(), true);
	}
	public static void installAPKAAPT() 
			throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException{
		DdmsDeviceSelectDialog dialog = new DdmsDeviceSelectDialog(MainFrame.getInstance());	
		dialog.setVisible(true);
		IDevice device = dialog.getSelectedDevice();
		if(device == null)return;
		JFileChooser chooser = new JFileChooser();
		int result = chooser.showOpenDialog(MainFrame.getInstance());
		if(result == JFileChooser.CANCEL_OPTION) return;
		DeviceUtils.installAPK(device, chooser.getSelectedFile().getAbsolutePath(), true);
	}
}
